package com.itheima.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

@Component
public class MybatisConfig {
    /*
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.itheima.bean"/>
        <property name="mapperLocations" value="classpath:com/itheima/mapper/StudentMapper.xml"/>
    </bean>
     */
    @Bean
    public SqlSessionFactoryBean SqlSessionFactoryBean(DataSource dataSource){
        // 获取工厂对象
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        // 设置工厂对象中的值
            // 设置需要起别名的实例类所在的包
        sqlSessionFactoryBean.setTypeAliasesPackage("com.itheima.bean");
            // 设置注入的datasource对象
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }

    /*
    * <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itheima.mapper"/>
      </bean>
    * */
    @Bean
    public MapperScannerConfigurer MapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        // 配置 扫描这个包下的所有的mapper接口 并创建动态代理对象 储存到spring容器中
        mapperScannerConfigurer.setBasePackage("com.itheima.mapper");
        return mapperScannerConfigurer;
    }
}
